﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admintractor_Panel : System.Web.UI.Page
{
    PanelBusiness panel = new PanelBusiness();
    ChanelBusiness channel = new ChanelBusiness();
    private int id = Helper.getQuery("id");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDrop();
            Option.bindDropdown(dropCategory, Option.pannel_category);
            Option.bindDropdown1(dropControl, Option.control,3);
            bindGV();
            if (id > 0)
            {
                Option.bindDropdown2(dropCell, Option.cell, 2);
                PanelesEntities p = panel.getItem(id);
                txtName.Text = p._name;
                dropChanel.SelectedValue = p._channelID.ToString();
                dropControl.SelectedValue = p._control;
                dropCell.SelectedValue = p._cellID.ToString();
                dropCategory.SelectedValue=p._category.ToString();
                txtPriority.Text = p._priority.ToString();
                txtFile1.Text = p._file1.ToString();
                txtFile2.Text = p._file2.ToString();
                if (p._displayAll.Trim().Equals("True") || p._displayAll.Trim().Equals("1"))
                { cb_DisplayALL.Checked = true; }
                if (p._displayContainer.Trim().Equals("True") || p._displayContainer.Trim().Equals("1"))
                {cb_DisplayContainer.Checked = true; }
                but_add.Text = "Sửa";
            }
            else
            {
                Option.bindDropdown2(dropCell, panel.getCell_All(), 2);
                but_add.Text = "Thêm mới";
            }
        }
    }
    public void bindDrop()
    {
        ArrayList items = channel.Menu();
        dropChanel.DataSource = items;
        dropChanel.DataTextField = "_name";
        dropChanel.DataValueField = "_id";
        dropChanel.DataBind();
        dropChanel.Items.Add(new ListItem("- Chọn một -", 0.ToString()));
        dropChanel.SelectedValue = 0.ToString();
    }
    public void bindGV()
    {
        GridView1.DataSource = panel.getPanel();
        GridView1.DataBind();
    }

    public string viewchannel(int chanel_id)
    {
        string tmp = "";
        ArrayList items = channel.getChanel();
        ChannelesEntities item = null;
        for (int i = 0; i < items.Count; i++)
        {
            item = (ChannelesEntities)items[i];
            if (item._id == chanel_id)
            {
                tmp = item._name;
                break;
            }
        }
            return tmp;
    }

    public string viewcell(int cell_id)
    {
        string tmp = "";
        string[,] items = Option.cell;
        for (int i = 0; i < 13; i++)
        {
            if (cell_id == int.Parse(items[i,0].ToString()))
            {
                tmp = items[i,1].ToString();
                break;
            }
        }
        return tmp;
    }

    protected void lbEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Panel.aspx?id=" + ((LinkButton)sender).CommandArgument);
    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        if (panel.delete(int.Parse(((LinkButton)sender).CommandArgument)))
        {
            Response.Redirect("Panel.aspx");
        }
        else
        {
            Label1.Text = "No delete";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        if (dropChanel.SelectedValue.Equals("0"))
        {
            GridView1.DataSource = panel.getPanel();
        }
        else
        {
            GridView1.DataSource = panel.getPanel_Chanel(int.Parse(dropChanel.SelectedValue));
        }
        GridView1.DataBind();
    }
    protected void dropChanel_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataSource = panel.getPanel_Chanel(int.Parse(dropChanel.SelectedValue.Trim()));
        GridView1.DataBind();
    }
    private bool checknumber()
    {
        bool flat = true;
        try
        {
            if (
               (int.Parse(txtFile1.Text.Trim()) > 0) &&
               (int.Parse(txtFile2.Text.Trim()) > 0) &&
               (int.Parse(txtPriority.Text.Trim()) > 0))
            {
                flat = true;
            }
            else { flat = false; }
        }
        catch (FormatException ex)
        {
            flat = false;
        }
        return flat;
    }
    private PanelesEntities Item()
    {
        PanelesEntities p = new PanelesEntities();
        p._name = txtName.Text.Trim();
        p._channelID = int.Parse(dropChanel.SelectedValue.Trim());
        p._control = dropControl.SelectedValue.Trim();
        p._cellID = int.Parse(dropCell.SelectedValue.Trim());
        p._category = int.Parse(dropCategory.SelectedValue.Trim());
        p._priority = int.Parse(txtPriority.Text.Trim());
        p._file1 = int.Parse(txtFile1.Text.Trim());
        p._file2 = int.Parse(txtFile2.Text.Trim());
        p._displayAll = cb_DisplayALL.Checked ? "1" : "0";
        p._displayContainer = cb_DisplayContainer.Checked ? "1" : "0";
        return p;
    }
    protected void but_add_Click(object sender, EventArgs e)
    {
        if (txtName.Text.Trim().Equals("") || txtPriority.Text.Trim().Equals("") || txtFile1.Text.Trim().Equals("") || txtFile2.Text.Trim().Equals("") ||
            dropChanel.SelectedValue.Trim().Equals("0") || dropCell.SelectedValue.Trim().Equals("0") ||
            dropCategory.SelectedValue.Trim().Equals("0") || dropControl.SelectedValue.Trim().Equals(" "))
        {
            Label1.Text = "Nhập thiếu thông tin";
        }
        else
        {
            if (this.checknumber())
            {
                if (id > 0)
                {
                    PanelesEntities pn = this.Item();
                    pn._id = id;
                    if (panel.edit(pn))
                    {
                        bindGV();
                        txtName.Text = ""; txtPriority.Text = ""; txtFile1.Text = ""; txtFile2.Text = "";
                        dropChanel.SelectedValue = "0"; dropCell.SelectedValue = "0"; dropCategory.SelectedValue = "0"; dropControl.SelectedValue = " ";
                    }
                    else
                    {
                        Label1.Text = "Xẩy ra nỗi trong quá trình thực hiện";
                    }
                }
                else
                {
                    PanelesEntities pn = this.Item();
                    if (panel.add(pn))
                    {
                        Response.Redirect("Panel.aspx");
                    }
                    else
                    {
                        Label1.Text = "Xẩy ra nỗi trong quá trình thực hiện";
                    }
                }
            }
            else
            {
                Label1.Text = "Nhập sai thông tin";
            }
        }
    }
}
